export function createRouterMap(routes, routerOptions = {}) {
  // console.log(routes, "这里是用户的参数");
  let pathMap = routerOptions;
  routes.forEach((router) => {
    addRouterRecode(router, pathMap);
  });

  return { pathMap };
}

function addRouterRecode(router, pathMap, parent) {
  // 路径 记录
  // debugger;
  let parentPath = "";
  if (parent && parent.path !== "/") {
    parentPath = parent.path;
  }

  let path = parent ? `${parentPath}${router.path}` : router.path;
  let recode = {
    path: router.path,
    name: router.name,
    component: router.component,
    parent,
  };

  // 添加,  添加过了不用添加
  if (!pathMap[path]) {
    pathMap[path] = recode;
  }

  // 有没有儿子
  if (router.children) {
    // 递归
    router.children.forEach((child) => {
      // 注意 parent
      addRouterRecode(child, pathMap, recode);
    });
  }
}

export function createRouter(recode, { path }) {
  // 定义结构
  let metched = [];
  if (recode) {
    // 可能有多个, 便利一下
    while (recode) {
      metched.unshift(recode);
      recode = recode.parent;
    }
  }

  return {
    path,
    metched,
  };
}
